package cn.wowjoy.pda.android_pharmacy.ui.activity.door.take;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityDoorTakeMedicineDetailBinding;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.doorPatientInfo.DoorPatientInfoBeen;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.DoorTakeMedicineDetailPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.DoorTakeMedicineDetailViewModel;

public class DoorTakeMedicineDetailActivity extends BaseActivity<ActivityDoorTakeMedicineDetailBinding, DoorTakeMedicineDetailViewModel, DoorTakeMedicineDetailPresenter> {
   private  ArrayList<DoorPatientInfoBeen> currently;
    private MDialog mDialog;
    private MDialog failSubmit;

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);
        binding.rvMedicineDetail.setLayoutManager(new LinearLayoutManager(this));

        binding.medicineDetailTitle.titleTextTv.setText("扫码取药");

        //禁止下拉刷新
        binding.rvMedicineDetail.setPullRefreshEnabled(false);
        binding.medicineDetailTitle.titleBackTv.setText("");
        binding.medicineDetailTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.medicineDetailTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != viewModel.datas && null != presenter.FitstData &&
                        viewModel.datas.size() < presenter.FitstData.size()){
                    mDialog = CreateDialog.exitDialog(DoorTakeMedicineDetailActivity.this, 5, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    },"退出取药单后，需重新开始操作");
                }else{
                    finish();
                }
            }
        });
        StringBuilder stringBuilder;
        if(presenter.type == 1){
             stringBuilder =new StringBuilder();
            for(String djjlid : presenter.DJJLIDs){
                stringBuilder.append(djjlid+".");
            }
            //处方号
            binding.tvMedicineDetailChufanghao.setText("合并处方号: "+stringBuilder.toString());
        }else{
            //处方号
            binding.tvMedicineDetailChufanghao.setText("处方号: "+presenter.DJJLID);
        }

        //患者科室
        binding.tvMedicineDetailPatientRoom.setText("科室： "+presenter.FitstData.get(0).getCFKDKS());
        //患者姓名
        binding.tvMedicineDetailName.setText("患者: "+presenter.FitstData.get(0).getBRDAXM());

        //开单时间
        binding.tvMedicineDetailTime.setText("开单时间："+presenter.FitstData.get(0).getCFKDSJ());
        binding.btMedicineDetailStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO:传递页面展现的药品集合过去
                // 传过去之前先检测当前药单的库位是否都存在
                List<DoorPatientInfoBeen> doorPatientInfoBeans = viewModel.checkKW();
                if(null != doorPatientInfoBeans && doorPatientInfoBeans.size() == 0){
                    Intent intent = new Intent(DoorTakeMedicineDetailActivity.this, DoorTakeProgressActivity.class);
                    //处方号
                    if(presenter.type == 1){
                        intent.putExtra("type",1);
                        intent.putExtra("DJJLIDs", (Serializable) presenter.DJJLIDs);
                    }else{
                        intent.putExtra("DJJLID",presenter.DJJLID);
                    }
                    intent.putExtra("DJJLID",presenter.DJJLID);
                    intent.putExtra("BRDAID",presenter.BRDAID);
                    intent.putExtra("total", viewModel.datas);
                    intent.putExtra("BRZYID", presenter.BRZYID);
                    startActivityForResult(intent, Contants.REQUEST_CODE);
                }else{
                   //这里进行强提示  退回到主页面
                    failSubmit = CreateDialog.failSubmit(DoorTakeMedicineDetailActivity.this, "取药失败", "当前"+doorPatientInfoBeans.get(0).getZZYPMC(), new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(DoorTakeMedicineDetailActivity.this, DoorTakeIndexActivity.class));
                            finish();
                        }
                    });
                }

            }
        });
        //TODO:获取RecycleView的总条目数
        if(presenter.FitstData != null){
            binding.tvMedicineDetailTotalNum.setText("共"+ viewModel.datas.size()+"种药品剩余");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                //TODO:用户未操作完全的药单 按BACK键返回  刷新界面数据
                currently = (ArrayList<DoorPatientInfoBeen>) data.getExtras().getSerializable("currently");
                Log.e("TAG", "onActivityResult: "+currently.size()  + "  "+currently.toString() );
                if(null != currently && currently.size()<viewModel.datas.size()){
                    viewModel.setWData(currently);
                    binding.tvMedicineDetailTotalNum.setText("共"+ viewModel.datas.size()+"种药品剩余");
                }
                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if(null != viewModel.datas && null != presenter.FitstData &&
                    viewModel.datas.size() < presenter.FitstData.size()){
                mDialog = CreateDialog.exitDialog(DoorTakeMedicineDetailActivity.this, 5, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                },"退出取药单后，需重新开始操作");
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null != mDialog){
            mDialog.cancel();
        }
        if(null != failSubmit){
            failSubmit.cancel();
        }
        PreferenceManager.getInstance().setDoorMedicineProgressTotal("0");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_door_take__medicine_detail;
    }

    @Override
    protected Class<DoorTakeMedicineDetailPresenter> getPresenterClass() {
        return DoorTakeMedicineDetailPresenter.class;
    }

    @Override
    protected Class<DoorTakeMedicineDetailViewModel> getViewModelClass() {
        return DoorTakeMedicineDetailViewModel.class;
    }
}
